var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
const nodeExternals = require('webpack-node-externals')
const path = require('path')

var isProduction = process.env.NODE_ENV === 'production';
var productionPluginDefine = isProduction ? [
  new webpack.DefinePlugin({'process.env': {'NODE_ENV': JSON.stringify('production')}})
] : [];
var clientLoaders = isProduction ? productionPluginDefine.concat([
  new webpack.optimize.DedupePlugin(),
  new webpack.optimize.OccurrenceOrderPlugin(),
  new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, sourceMap: false })
]) : [];

const commonLoader = [{
    test: /\.js$/,
    loader: 'babel-loader',
    query: {
        presets: ['env', 'react']
    }
},{
  test: /\.css$/,
  use: ExtractTextPlugin.extract({
    fallback: "style-loader",
    use: "css-loader"
  })
}]

module.exports = [
  {
    entry: './src/server.js',
    output: {
      path: path.join(__dirname, './dist'),
      filename: 'server.js',
      libraryTarget: 'commonjs2',
      publicPath: '/'
    },
    target: 'node', // in order to ignore built-in modules like path, fs, etc
    node: {
      console: false,
      global: false,
      process: false,
      Buffer: false,
      __filename: false,
      __dirname: false
    },
    externals: nodeExternals(),
    plugins: productionPluginDefine,
    module: {
      rules: commonLoader
    }
  },
  {
    entry: './src/app/browser.js',
    output: {
      path: path.join(__dirname, './dist/assets'),
      publicPath: '/',
      filename: 'bundle.js'
    },
    module: {
      rules: commonLoader
    },
    resolve: {
      extensions: ['.js', '.jsx']
    },
    plugins: clientLoaders.concat([
      new ExtractTextPlugin('index.css', {
        allChunks: true
      })
    ])
  }
];